﻿using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Routine.API.Data;
using Routine.API.Services;
using System;

namespace Routine.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            /*
            * 内容协商：
            * 针对一个响应，当有多种表述格式的时候，选取最佳的一个表述，例如 application/json、application/xml
            * 
            * Accept Header 指明服务器输出格式，对应 ASP.NET Core 里的 Output Formatters
            * 如果服务器不支持客户端请求的媒体类型（Media Type），返回状态码406
            * 
            * Content-Type Header 指明服务器输入格式，对应 ASP.NET Core 里的 Input Formatters
            */



            services.AddControllers(options =>
            {
                //启用406状态码 不支持的返回格式
                options.ReturnHttpNotAcceptable = true;

                //OutputFormatters为数组，默认支持的返回格式未第一个JSON
                //添加对输出XML格式的支持 方式1
                //options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());

                //如果在 Index 0 位置插入对 XML 格式的支持，那么默认输出格式是 XML
                //options.OutputFormatters.Insert(0, new XmlDataContractSerializerOutputFormatter());
            })
                .AddXmlDataContractSerializerFormatters(); //XML 序列化和反序列化工具 方式2;

            services.AddDbContext<RoutineDbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            });

            services.AddScoped<ICompanyRepository, CompanyRepository>();
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //自定义异常信息
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("Unexcepted Error!");
                    });
                });
            }
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
